package ink.lovejinhu.leetcode;

/**
 * @Author: jin
 * @Date: 2021/9/13 11:00
 * @Version: V1.0
 * @Slogan: Great people are not born great, but show their greatness in the process of growing up
 **/
public class Solution_0917 {
    /**
     * 仅仅只反转字母
     *
     * @param s
     * @return
     */
    public String reverseOnlyLetters(String s) {

        char[] chars = s.toCharArray();
        /**
         * 双指针
         */
        for (int i = 0, j = s.length() - 1; i < j; ) {
            boolean left = Character.isLetter(chars[i]);
            boolean right = Character.isLetter(chars[j]);
            if (left && right) {
                /**
                 * 交换两个字母
                 */
                char temp = chars[i];
                chars[i] = chars[j];
                chars[j] = temp;
                i++;
                j--;
                continue;
            }
            if (!left && !right) {
                i++;
                j--;
                continue;
            }
            if (left) {
                j--;
            } else {
                i++;
            }


        }
        return new String(chars);
    }
}
